package com.luckilyswl.httpdemo.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.luckilyswl.httpdemo.R;
import com.luckilyswl.httpdemo.bean.LaughData;


public class LaughAdapter extends RecyclerView.Adapter<LaughAdapter.ViewHolder> {

    private LaughData dataBeans;

    public LaughAdapter(Context context, LaughData dataBeanList){
        dataBeans = dataBeanList;
        this.mContext = context;
    }


    private Context mContext;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(mContext == null){
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_adapter_list,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final LaughData.ResultBean.DataBean dataBean = dataBeans.getResult().getData().get(position);

        holder.textContent.setText(dataBean.getContent());
        holder.textTime.setText(dataBean.getUpdatetime());
//        holder.textUnixTime.setText(dataBean.getUnixtime());
    }

    @Override
    public int getItemCount() {
        if(dataBeans == null){
            return 0;
        }else if(dataBeans.getResult() == null){
            return 0;
        }else if(dataBeans.getResult().getData() == null){
            return 0;
        }
        int count =  dataBeans.getResult().getData().size();

        Log.e("LaughAdapter", "count = " + count);

        return count;
    }

    public void ChangeData(LaughData laughData){
        this.dataBeans = laughData;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        CardView cardView;
        TextView textContent;
        TextView textTime;
        TextView textUnixTime;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = (CardView) itemView;
            textContent = itemView.findViewById(R.id.text_content);
            textTime = itemView.findViewById(R.id.text_time);
//            textUnixTime = itemView.findViewById(R.id.text_unix_time);
        }
    }
}
